package com.gherasoft.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.gherasoft.model.FolderItem;
import com.gherasoft.view.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * User: sckomoroh
 * Date: 3/22/13
 * Time: 9:54 AM
 */
public class OpenFileAdapter extends BaseAdapter
{
    private String currentPath = "/mnt/sdcard";
    private List<FolderItem> folderItems = new ArrayList<FolderItem>();
    private Context context;

    public OpenFileAdapter(Context context)
    {
        this.context = context;
        buildAdapter();
    }

    @Override
    public int getCount()
    {
        return folderItems.size();
    }

    @Override
    public Object getItem(int i)
    {
        return folderItems.get(i);
    }

    @Override
    public long getItemId(int i)
    {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
        FolderItem folderItem = folderItems.get(i);

        if (folderItem.isFolder())
        {
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.open_file_folder_item, null);

            TextView folderNameTextView = (TextView)view.findViewById(R.id.folderNameTextView);
            folderNameTextView.setText(folderItem.getName());
        }
        else
        {
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.open_file_item, null);

            TextView folderNameTextView = (TextView)view.findViewById(R.id.fileNameTextView);
            folderNameTextView.setText(folderItem.getName());
        }

        return view;
    }

    public String getCurrentFolder()
    {
        return this.currentPath;
    }

    public String getFullName(int pos)
    {
        FolderItem fi = folderItems.get(pos);

        return currentPath + "/" + fi.getName();
    }

    public void tryRebuild(int pos)
    {
        FolderItem fi = folderItems.get(pos);

        if (!fi.isFolder())
        {
            return;
        }

        if (pos != 0)
        {
            currentPath += "/" + fi.getName();
        }
        else
        {
            int index = currentPath.lastIndexOf("/");
            currentPath = currentPath.substring(0, index);
        }

        buildAdapter();

        notifyDataSetChanged();
    }

    private void buildAdapter()
    {
        folderItems.clear();

        File f = new File(currentPath);
        File[] files = f.listFiles();

        folderItems.add(new FolderItem("..", true));

        for (File ff : files)
        {
            if (ff.isDirectory())
            {
                folderItems.add(new FolderItem(ff.getName(), true));
            }
        }

        for (File ff : files)
        {
            if (!ff.isDirectory())
            {
                folderItems.add(new FolderItem(ff.getName(), false));
            }
        }
    }
}
